/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package plar.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CollisionManager {

	public HashMap<Element, List<Element>> collisions;

	public CollisionManager() {
		collisions = new HashMap<Element, List<Element>>();

	}

	public void addCollision(Element e1, Element e2) {
		if (!isColliding(e1, e2)) {
			collisionList(e1).add(e2);
			collisionList(e2).add(e1);
		}
	}

	public void addCollisionList(Element e) {
		collisions.put(e, new ArrayList<Element>());
	}

	private List<Element> collisionList(Element e1) {
		return collisions.get(e1);
	}

	public void delCollisionList(Element e) {
		collisions.remove(e);
		for (List<Element> l : collisions.values()) {
			if (l.contains(e))
				l.remove(e);
		}

	}

	public boolean isColliding(Element e1, Element e2) {
		if (collisionList(e1).contains(e2) || collisionList(e2).contains(e1))
			return true;
		else
			return false;
	}

	public void removeCollision(Element e1, Element e2) {
		if (isColliding(e1, e2)) {
			collisionList(e1).remove(e2);
			collisionList(e2).remove(e1);
		}
	}

}
